<?php

App::uses('AdminController', 'Controller');

class AdminNewsController extends AdminController {
	
	public $uses = array("News");
	
	public $paginate = array(
		'limit' => ITEMS_PER_PAGE,
		'order' => 'date DESC, id DESC'
	);
	
	public function index() {
		$this->set("news", $this->paginate('News'));
	}
	
	public function create() {
		$this->set("news", $this->News->create());
		$this->render('form');
	}
	public function edit($id) {
		$this->set("news", $this->News->findById($id));
		$this->render('form');
	}
	
	public function save() {
		if($this->request->is('post')) {
			$data = $this->data;
			
			$this->News->create();
			$this->News->set($data);
			
			$this->set("news", $data);
			if($this->News->validates()) {
				if($this->News->save($data)) {
					$this->setFlash('Vest je sačuvana.', 'ok');
					$this->redirect('/admin_news/edit/' . $this->News->id);
				}else{
					$this->setFlash('Vest nije sačuvana.');
					return $this->render("form");
				}
			}else{
				$this->setFlash('Vest nije sačuvana. Uneti podaci nisu validni.');
				return $this->render("form");
			}
		}
		
		$this->redirect("/admin_news");
	}
	
}
